package com.bilibili.juc.cf;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;

import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @auther zzyy
 * @create 2022-01-17 18:44
 */
public class CompletableFutureOfThenCombine2Demo {
    public static void main(String[] args) {
        //========================================================================
        System.out.println(CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName()+"\t -----A come in " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}
            System.out.println(Thread.currentThread().getName()+"\t -----A over   " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            return 10;
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName()+"\t -----B come in " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
            System.out.println(Thread.currentThread().getName()+"\t -----B over    " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            return 20;
        }), (x, y) -> {
            System.out.println(Thread.currentThread().getName()+"\t -----结果开始合并1    " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            return x + y;
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName()+"\t -----C come in " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
            System.out.println(Thread.currentThread().getName()+"\t -----C over    " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            return 30;
        }), (x, y) -> {
            System.out.println(Thread.currentThread().getName()+"\t -----结果开始合并2    " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));

            return x + y;
        }).join());//60

    }
    /*
ForkJoinPool.commonPool-worker-2	 -----B come in 2024-11-18 15:44:19
ForkJoinPool.commonPool-worker-3	 -----C come in 2024-11-18 15:44:19
ForkJoinPool.commonPool-worker-1	 -----A come in 2024-11-18 15:44:19
ForkJoinPool.commonPool-worker-2	 -----B over    2024-11-18 15:44:20
ForkJoinPool.commonPool-worker-3	 -----C over    2024-11-18 15:44:20
ForkJoinPool.commonPool-worker-1	 -----A over   2024-11-18 15:44:21
ForkJoinPool.commonPool-worker-1	 -----结果开始合并1    2024-11-18 15:44:21
ForkJoinPool.commonPool-worker-1	 -----结果开始合并2    2024-11-18 15:44:21
60
     */
}
